<template>
  <div class="tree-card">
    <el-card>
      <div class="app-container">
        <!-- 组织架构 -->
        <TreeTools
          :treeNode="company"
          :IsRoot="true"
          @addDepts="addDepts"
        ></TreeTools>
        <!-- 放置一个el-tree  -->
        <el-tree
          :default-expand-all="true"
          :data="departs"
          :props="defaultProps"
          style="width: 100%, hight:40px"
        >
          <!-- 传入插槽  作用域插槽 -->
          <TreeTools
            @addDepts="addDepts"
            @delDepts="getDepartments"
            @editDepts="editDepts"
            slot-scope="{ data }"
            :treeNode="data"
          ></TreeTools>
        </el-tree>
      </div>
    </el-card>
    <AddDept
      :show-dialog.sync="showDialog"
      :treeNode="NodeInfo"
      @addDepts="getDepartments"
      ref="addref"
    >
    </AddDept>
  </div>
</template>

<script>
import TreeTools from './components/tree-tools'
import { getDepartments } from '@/api/departments'
import { tranListToTreeData } from '@/utils/index'
import AddDept from './components/add-dept.vue'
export default {
  components: {
    TreeTools,
    AddDept
  },
  data() {
    return {
      company: {}, // 就是头部的数据结构
      departs: [],
      defaultProps: {
        label: 'name' // 表示 从这个属性显示内容
      },
      showDialog: false,
      NodeInfo: {}
    }
  },
  created() {
    this.getDepartments() // 调用自身的方法
  },
  methods: {
    async getDepartments() {
      const result = await getDepartments()
      this.company = { name: result.companyName, manager: '负责人' } // 这里定义一个空串  因为 它是根 所有的子节点的数据pid 都是 ""
      this.departs = tranListToTreeData(result.depts, '')
    },

    addDepts(data) {
      this.showDialog = true
      this.NodeInfo = data
    },
    editDepts(node) {
      this.showDialog = true //  弹出层
      //   同时记录两个节点
      this.NodeInfo = node

      // console.log(this.$refs.addref)
      this.$refs.addref.getDeparDetail(node.id)
      //  不在父组件调用接口,而是子组件有获取bum,部门详情的方法
    }
  }
}
</script>

<style scope>
</style>
